package list

type arrayList struct {
	list []interface{}
}

// 创建一个数组列表
func NewArrayList() *arrayList {
	return &arrayList{make([]interface{}, 0, 10)}
}

func (this *arrayList) Add(e interface{}) {
	this.list = append(this.list, e)
}

func (this *arrayList) AddAt(i int, e interface{}) {
	this.list = append(this.list, e)
	if i >= len(this.list) {
		return
	}
	if i < 0 {
		i = 0
	}
	copy(this.list[i+1:], this.list[i:])
	this.list[i] = e
}

func (this *arrayList) Clear() {
	this.list = this.list[:0]
}

func (this *arrayList) Contains(e interface{}) bool {
	for _, t := range this.list {
		if t == e {
			return true
		}
	}
	return false
}

func (this *arrayList) Get(i int) interface{} {
	if i < 0 || i >= len(this.list) {
		return nil
	}
	return this.list[i]
}

func (this *arrayList) IndexOf(e interface{}) int {
	for i, t := range this.list {
		if t == e {
			return i
		}
	}
	return -1
}

func (this *arrayList) IsEmpty() bool {
	return len(this.list) <= 0
}

func (this *arrayList) Remove(e interface{}) bool {
	for i, t := range this.list {
		if t == e {
			copy(this.list[i:], this.list[i+1:])
			this.list = this.list[:len(this.list)-1]
			return true
		}
	}
	return false
}

func (this *arrayList) SetAt(i int, e interface{}) bool {
	if i < 0 || i >= len(this.list) {
		return false
	}
	this.list[i] = e
	return true
}

func (this *arrayList) Size() int {
	return len(this.list)
}

func (this *arrayList) Capacity() int {
	return cap(this.list)
}
